// pages/heat/heat.js
const app = getApp();
import util from '../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    param: {
      pageNumber: 1,
      pageSize: 10
    },
    orgId:'',
    list:[],
    calorificValue:0,
    todayValue:0,
  },
  getlist(append){
  var that = this
  app.req({
    url: "orgmini/calorificDetail",
    data: {
      orgId: that.data.orgId,
      pageNumber: that.data.param.pageNumber,
      pageSize: that.data.param.pageSize
    },
    success: function (res) {
      if (200 == res.code) {
        that.toggle("calorificValue", res.data.calorificValue);
        that.toggle("todayValue", res.data.todayValue);
        var now = new Date()
        var year = now.getFullYear()
        console.log(now)
        var list = res.data.page.rows
        for (var item of list) {
          item.createTime = item.createTime.replace(/\-/g, "/")
          var date = new Date(item.createTime)
          item.index = date.getTime();
          if (date.getFullYear() == year) {
            item.year = ''
          } else {
            item.year = date.getFullYear() + '年'
          }

          item.week = util.getWeek(date)
          item.actTime = item.createTime.substring(5, 7) + '月' + item.createTime.substring(8, 10) + '日'
        }
        list.sort(that.sortRule)
        if (!list || list.length < that.data.param.pageSize) {
          that.toggle("pageEnd", true);
        }
        that.toggle("list", append ? that.data.list.concat(list) : list);
        console.log('拼接',that.data.list)
        for (var item of that.data.list) {
          item.actStartDate = item.createTime.substring(0, 10)
        }
        var arr = that.sortArr(that.data.list, 'actStartDate')
        that.toggle("lists", arr);
        console.log('明细',that.data.lists)
      }
    }
  });
},
  sortRule(a, b) {
    return b.index - a.index;
  },
  // 传入一个数组
  // 按照特定方式格式化
  sortArr(arr, str) {
    console.log(arr)
    var _arr = [],
      _t = [],
      _tmp;
    arr = arr.sort(function (a, b) {
      var s = a[str],
        t = b[str];

      return s > t ? -1 : 1;
    });

    if (arr.length) {
      _tmp = arr[0][str];
    }
    for (var i in arr) {
      if (arr[i][str] === _tmp) {
        _t.push(arr[i]);
      } else {
        _tmp = arr[i][str];
        _arr.push(_t);
        _t = [arr[i]];
      }
    }
    _arr.push(_t);
    return _arr;
  },
  toggle(type, val) {
    this.setData({
      [type]: val
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      orgId: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getlist()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (!that.data.pageEnd) {
      that.toggle("param.pageNumber", that.data.param.pageNumber + 1);
      that.getlist(true);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})